import Vue from 'vue'
import VueRouter from 'vue-router'
// import HomeView from '../views/HomeView.vue'
import { getToken } from "@/utils/storage"
Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/login.vue')
  },
  {
    path: '/',
    name: 'layout',
    redirect: 'home',
    component: () => import('../views/layout.vue'),
    children: [
      {
        path: '/home',
        name: 'home',
        component: () => import('../views/Home.vue'),
      },
      {
        path: '/users',
        name: 'userList',
        component: () => import('../views/userList.vue'),
      },
      {
        path: '/roles',
        name: 'userQxList',
        component: () => import('../views/quanxianguanli/userQxList.vue'),
      },
      {
        path: '/rights',
        name: 'qxList',
        component: () => import('../views/quanxianguanli/qxList.vue'),
      },
      {
        path: '/goods',
        name: 'goods',
        component: () => import('../views/shop/goods.vue'),

      },
      {
        path: '/params',
        name: 'params',
        component: () => import('../views/shop/params.vue'),

      },
      {
        path: '/categories',
        name: 'categories',
        component: () => import('../views/shop/categories.vue'),

      },
      {
        path: '/goods/add',
        name: 'add',
        component: () => import('../views/shop/add.vue'),
      },
      {
        path: '/orders',
        name: 'indent',
        component: () => import('../views/indent/indent.vue'),
      },
      {
        path: '/reports',
        name: 'reports',

        component: () => import('../views/reports/reports.vue'),

      },

    ]
  }
]

const router = new VueRouter({
  routes
})
const whiteList = ['/login']

router.beforeEach((to, from, next) => {
  const token = getToken()
  if (token) {
    next()
    return
  }
  if (whiteList.includes(to.path)) {
    next()
    return
  }
  next('/login')
})
//保存原型对象的Push
let originPush = VueRouter.prototype.push
let originReplace = VueRouter.prototype.replace
//重写push方法
VueRouter.prototype.push = function (location, res, rej) {
  if (res && rej) {
    originPush.call(this, location, res, rej)
  } else {
    originPush.call(this, location, () => { }, () => { })
  }
}
//重写replace方法
VueRouter.prototype.replace = function (location, res, rej) {
  if (res && rej) {
    originReplace.call(this, location, res, rej)
  } else {
    originReplace.call(this, location, () => { }, () => { })
  }
}
export default router
